<template>
  <div>
    <el-dialog title="查看" :close-on-click-modal="false" :visible.sync="seeVisible">
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="80px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input disabled v-model="dataForm.roleName" placeholder="角色名称"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input disabled v-model="dataForm.remark" placeholder="备注"></el-input>
        </el-form-item>
        <el-form-item size="mini" label="授权">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="menuId"
            ref="menuListTree"
            :default-expand-all="true"
            show-checkbox
          >
          </el-tree>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { treeDataTranslate } from '@/utils';
export default {
  data() {
    return {
      seeVisible: false,
      menuList: [],
      menuListTreeProps: {
        label: 'name',
        children: 'children',
        disabled: this.disabledFn,
      },
      dataForm: {
        id: 0,
        roleName: '',
        remark: '',
      },
      dataRule: {
        roleName: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
      },
      tempKey: -666666, //
      user: {
        roleIdList: null,
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl('/sys/menu/list'),
        method: 'get',
        params: this.$http.adornParams(),
      })
        .then(({ data }) => {
          this.menuList = treeDataTranslate(data, 'menuId');
        })
        .then(() => {
          this.seeVisible = true;
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields();
            this.$refs.menuListTree.setCheckedKeys([]);
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams(),
            }).then(({ data }) => {
              this.user.roleIdList = data.user.roleIdList;
              // console.log(this.user.roleIdList);
              let nums = this.user.roleIdList;
              console.log(nums);
              if (nums[0] == 5 && nums[1] == 1) {
                this.$http({
                  url: this.$http.adornUrl(`/sys/role/info/1`),
                  method: 'get',
                  params: this.$http.adornParams(),
                }).then(({ data }) => {
                  if (data && data.code === 0) {
                    this.dataForm.roleName = data.role.roleName;
                    this.dataForm.remark = data.role.remark;
                    var idx = data.role.menuIdList.indexOf(this.tempKey);
                    if (idx !== -1) {
                      data.role.menuIdList.splice(idx, data.role.menuIdList.length - idx);
                    }
                    this.$refs.menuListTree.setCheckedKeys(data.role.menuIdList);
                  }
                });
              } else if (nums[0] == 1 && nums[2] == undefined) {
                this.$http({
                  url: this.$http.adornUrl(`/sys/role/info/1`),
                  method: 'get',
                  params: this.$http.adornParams(),
                }).then(({ data }) => {
                  if (data && data.code === 0) {
                    this.dataForm.roleName = data.role.roleName;
                    this.dataForm.remark = data.role.remark;
                    var idx = data.role.menuIdList.indexOf(this.tempKey);
                    if (idx !== -1) {
                      data.role.menuIdList.splice(idx, data.role.menuIdList.length - idx);
                    }
                    this.$refs.menuListTree.setCheckedKeys(data.role.menuIdList);
                  }
                });
              } else if (nums[0] == 5 && nums[2] == undefined) {
                this.$http({
                  url: this.$http.adornUrl(`/sys/role/info/5`),
                  method: 'get',
                  params: this.$http.adornParams(),
                }).then(({ data }) => {
                  if (data && data.code === 0) {
                    this.dataForm.roleName = data.role.roleName;
                    this.dataForm.remark = data.role.remark;
                    var idx = data.role.menuIdList.indexOf(this.tempKey);
                    if (idx !== -1) {
                      data.role.menuIdList.splice(idx, data.role.menuIdList.length - idx);
                    }
                    this.$refs.menuListTree.setCheckedKeys(data.role.menuIdList);
                  }
                });
              } else if (nums[0] == 1 && nums[1] == 5) {
                this.$http({
                  url: this.$http.adornUrl(`/sys/role/info/1`),
                  method: 'get',
                  params: this.$http.adornParams(),
                }).then(({ data }) => {
                  if (data && data.code === 0) {
                    this.dataForm.roleName = data.role.roleName;
                    this.dataForm.remark = data.role.remark;
                    var idx = data.role.menuIdList.indexOf(this.tempKey);
                    if (idx !== -1) {
                      data.role.menuIdList.splice(idx, data.role.menuIdList.length - idx);
                    }
                    this.$refs.menuListTree.setCheckedKeys(data.role.menuIdList);
                  }
                });
              } else {
                this.$http({
                  url: this.$http.adornUrl(`/sys/role/info/nums`),
                  method: 'get',
                  params: this.$http.adornParams(),
                }).then(({ data }) => {
                  if (data && data.code === 0) {
                    this.dataForm.roleName = data.role.roleName;
                    this.dataForm.remark = data.role.remark;
                    var idx = data.role.menuIdList.indexOf(this.tempKey);
                    if (idx !== -1) {
                      data.role.menuIdList.splice(idx, data.role.menuIdList.length - idx);
                    }
                    this.$refs.menuListTree.setCheckedKeys(data.role.menuIdList);
                  }
                });
              }
            });
          }
        });
    },
    disabledFn() {
      return true;
    },
  },
};
</script>
